<?php
namespace app\erp\controller;

use app\erp\model\Sitedb;

class Site extends Base
{

    protected $cols = array(
        array(
            'field' => 'site_id',
            'width' => 40,
            'title' => 'ID',
            'type' => 'checkbox',
            'fixed' => 'left'
        ),
        array(
            'field' => 'site_name',
            'width' => 160,
            'title' => '分站名称',
            'sort' => 'true'
        ),
        array(
            'field' => 'site_http',
            'width' => 240,
            'title' => '分站域名'
        ),
        array(
            'field' => 'master_site_goods_text',
            'width' => 100,
            'title' => '主站产品'
        ),
        array(
            'field' => 'goods_alone_text',
            'width' => 100,
            'title' => '产品独立'
        ),
        array(
            'field' => 'goods_audit_text',
            'width' => 100,
            'title' => '主站审核'
        ),
        array(
            'field' => 'create_time',
            'width' => 120,
            'title' => '加入时间',
            'sort' => 'true'
        )
    );

    public function index()
    {
        array_push($this->cols, array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#site_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }

    /**
     *
     * @todo 列表
     */
    public function ajaxSite()
    {
        $request = request();
        $param = $request->param();
        $sitedb = new Sitedb();
        $sitedb->param = $param;
        $return = $sitedb->ajaxSelect($param);
        $this->layuiReturn($return['data'], $return['count']);
    }

    /**
     *
     * @todo 表单
     */
    public function tplForm()
    {
        $request = request();
        $param = $request->param();
        $data = array();
        $site_id = '';
        if (isset($param['site_id']) && ! empty($param['site_id'])) {
            // 获取数据
            $sitedb = new Sitedb();
            $data = $sitedb->ajaxFind($param['site_id']);
            $site_id = $param['site_id'];
        }
        $this->assign('data', json_encode($data));
        $this->assign('site_id', $site_id);
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $request = request();
        $param = $request->param();
        $sitedb = new Sitedb();
        $return = $sitedb->ajaxSave($param);
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, - 1);
        } else {
            $this->layuiReturn('保存成功');
        }
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $request = request();
        $param = $request->param();
        $sitedb = new Sitedb();
        $sitedb->param = $param;
        $sitedb->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }
    public function ajaxRestore(){
        $request = request();
        $param = $request->param();
        $sitedb = new Sitedb();
        $sitedb->param = $param;
        $sitedb->ajaxRestore();
        $this->layuiReturn('数据恢复成功',0);
    }

    /**
     *
     * @todo 获取单条数据
     */
    public function ajaxOneData()
    {
        $request = request();
        $param = $request->param();
        $sitedb = new Sitedb();
        $data = $sitedb->ajaxFind($param['site_id']);
        $this->layuiReturn($data, 1);
    }

    public function recycle()
    {
        array_push($this->cols, array(
            'field' => 'delete_time',
            'width' => 120,
            'title' => '删除时间'
        ),array(
            'fixed' => 'right',
            'width' => 80,
            'toolbar' => '#recycle_site_tool'
        ));
        $this->assign('cols', json_encode($this->cols));
        return $this->fetch();
    }
}